<template>
  <div class="page">
    <!-- 表单 -->
    <commpVxeTable
      :key="reCommpVxeTable"
      :config="dataTableConfig"
      :columns="columns"
      :formItem="fromItem"
      :isCanWrap='true'
      :conditionBtnRow="true"
      @checkboxChange="checkboxChange"
      ref="administrativeOffice"
      @searchEvent="searchEvent"
      @resetEvent="resetEvent"
      >
      <template v-slot:externalOperations="{data}"  >
          <vxe-form-item>
              <template>
                  <a-button class="blue-btn" @click="batchPrint">批量打印</a-button>
              </template>
          </vxe-form-item>
      </template>
        <template v-slot:operation_btn="{row}" data-desc="操作">
          <a-form layout="inline">
            <a-form-item>
                  <a-button class="blue-btn" @click="handlerEdit(row)">办理</a-button>
            </a-form-item>
            <a-form-item>
                  <a-button class="blue-btn" @click="printClick(row)">打印</a-button>
            </a-form-item>
            <!-- <a-form-item>
                  <a-button class="red-btn"  @click="handlerDelete(row)">删除</a-button>
            </a-form-item> -->
          </a-form>
        </template>
        <template v-slot:data1_default="{row}">
            <span style="padding-right: 21;" v-if="row.fjcyxm.length == 2">
                <span v-for="(trem,tI) in row.fjcyxm.split('')" :key="tI" >
                    {{trem}}
                    <span v-if="tI+1 !== row.fjcyxm.split('').length">&nbsp;&nbsp;</span>
                </span>
            </span>
            <span style="padding-right: 21;" v-else>{{ row.fjcyxm }}</span>
        </template>
    </commpVxeTable>
    
    <printInspector ref="printRefName" />
    <increase ref="increaseRef" />
  </div>
</template>

<script>
//流程接口
import {
  flowJobPage,
  queryJobByJobId,
  saveFlow,
  submitFlow,
  returnUpperFlowJob,
  queryBatchYbjcyList,
  downMyQm
} from "../api/prepareinspectorm";
import  commpVxeTable   from '@/components/commp-vxe-table/commp-vxe-table.vue'
import comVxeTable from "@/views/everydayCheck/components/vxe-table/com-vxe-table";
import EvaluationForm from "@/views/everydayCheck/prepareinspectorm/EvaluationForm";
import printInspector from './printInspector-new.vue'
import YchConstant from "@/api/YchConstant"; //接口模块配置项
import increase from './increase'
export default {
  components: { comVxeTable, EvaluationForm, printInspector,commpVxeTable, increase },
  name: "dengjiPage",
  props: {
    type: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      reCommpVxeTable: '',
      dataTableConfig: {
        api: YchConstant.SPZX_SPCY + '/flow-ybjcy/flowJobPage',
        rowId: 'id',
        paramet: {}
      },
      columns: [
        {
          type: "checkbox",
          width: 60,
          fixed: 'left'
        },{
          field: "fjcyxm",
          title: "姓名",
          titleInterval:true,
          slots: {
              header:'data1_header',
              // default:'data1_default',
          },
          width: 100,
        },
        {
          field: "fgzdw",
          title: "工作单位",
          // width: "150",
        },
        {
          field: "fqymc",
          title: "被审评单位",
          // width: "150",
        },
        {
          field: "fjclx",
          title: "审评类型",
          // width: "150",
        },
        {
          field: "fjcsj",
          title: "审评时间",
          width: 200,
        },
        {
          field: "fhjmc",
          title: "环节名称",
          width: 100,
        },
        // {
        //   field: "fdbr",
        //   title: "待办人",
        //   // width: "150",
        // },
        {
          field: "ffsrmc",
          title: "发送人",
          width: 100,
        },
        {
          field: "ffssj",
          title: "发送时间",
          width: 170,
        },
        // {
        //   field: "fblzt",
        //   title: "办理状态",
        //   // fixed:"right",
        //   // width: "150",
        // },
        {
          field: "operate",
          title: "操作",
          fixed: "right",
          width: "180",
          slots: {
          // 使用插槽模板渲染
              default: 'operation_btn',
          },
        },
      ],
      queryParams: {},
      config: {},
      data: [],
      allAlign: null,
      formData: {
        fjcyxm: "",
        fhjmc: "",
      },
      fromItem: [
        {
          model: "fjcyxm",
          label: "姓名",
          interval:true,
        },
        {
          model: "fhjmc",
          label: "环节名称",
          placeholder: '环节名称',
          type:'Select',
          dictionaryType:'预备审评员考评流程环节'
        },
      ],
      page: flowJobPage,
      initialize: {
        fblzt: "待办",
        fhjmc: this.type,
      },
      // fndszbg 填写 大文本
      form: {},
      model: {},
      modal: {
        visabled: false,
        width: "100vw",
        height: "100vh",
        title: "",
      },
      jobid: "",
      ybid: "",
      nextNode: "",
      currentNode: this.type,
      saveForm: {},
      ybkp: null,
      nextNodeDbrList:[],
      checkboxArr:[],
      returnLoading: false,
      loading: false
    };
  },
  created() {},
  mounted() {},

  methods: {
    //选择表格
    checkboxChange(val,records){
      //console.log(val,records)
      this.checkboxArr = val
      //console.log(this.checkboxArr)

    },
            // 搜索
        searchEvent(){
            this.dataTableConfig.paramet = {
                ...this.dataTableConfig.paramet,
            }
        },
        // 重置
        resetEvent(){
            this.dataTableConfig.paramet = {
                ...this.dataTableConfig.paramet,
            }
        },
    batchPrint(){
      if(this.checkboxArr.length){
        let idd = []
        this.checkboxArr.forEach(item => {
            idd.push(item.ybid)
        });
        queryBatchYbjcyList({ids:idd.toString()}).then(res=>{
          if(res.code == 200){
            var signatureData = []
            var list = []
            res.result.forEach((item,index) => {
                downMyQm({id:item.fzzid}).then(res=>{
                  if(res.code){
                    item['signature'] = null
                  } else {
                    item['signature'] = "data:image/png;base64," +
                    btoa( new Uint8Array(res).reduce( (data, byte) => data + String.fromCharCode(byte), "" ))
                  }
                })
                list[index] = item
                if(signatureData.indexOf(item.id) == -1){
                  signatureData.push(item.id)
                }
                if(signatureData.length == this.checkboxArr.length){
                  this.$refs.printRefName.getData(list)
                }
            });


          }else{
            this.$message.warning(res.messsage)
          }
        })
      }else{
        this.$message.warning('请选择打印项')
      }

    },
    printClick(val){
      //console.log(val)
      if(val.ybid){
        queryBatchYbjcyList({ids:val.ybid}).then(res=>{
          if(res.code == 200){
            var signatureData = []
            var list = []
            res.result.forEach((item,index) => {
                downMyQm({id:item.fzzid}).then(res=>{
                  //console.log('下载签名---',res)
                  if(res.code){
                    item['signature'] = null
                  } else {
                    item['signature'] = "data:image/png;base64," +
                    btoa( new Uint8Array(res).reduce( (data, byte) => data + String.fromCharCode(byte), "" ))
                  }
                    list[index] = item
                    if(signatureData.indexOf(item.id) == -1){
                      signatureData.push(item.id)
                    }
                    if(signatureData.length == 1){
                      this.$refs.printRefName.getData(list)
                    }
                })
            });
            //console.log(signatureData)

            // this.$refs.printRefName.getData(res.result)
          }else{
            this.$message.warning(res.messsage)
          }
        })
      }else{
        this.$message.warning('未获取到当前项')
      }
    },
    async handlerEdit(row) {
      this.$refs.increaseRef.getData(row)
    },
    handlerDelete(row) {
      //console.log(row);
    },
    handleModalOk() {
      //console.log("確定");
    },
    handleClose() {
      //console.log("取消");
    },
    handleAdd() {
      this.modal.visabled = true;
    },
    handlerSave() {
      const form = this.getQueryPamas();
      saveFlow(form)
        .then((res) => {
          if (res.success) {
            this.$VxeMessage({ content: res.message, status: "success" });
            // this.handlerEdit(form);
          } else {
            this.$VxeMessage({ content: res.message, status: "error" });
          }
        })
        .catch((error) => {
          this.$VxeMessage({ content: "未知异常", status: "error" });
        });
    },
    handlerSubmit() {
      const form = this.getQueryPamas();
      //console.log('this.type',this.type)
      //console.log('form---',form)
      if (this.type.indexOf("个人总结") !== -1) {
        //console.log('form.map.fcdjcrw',form.map.fcdjcrw)
        //console.log('form.map.fsxjcyzj',form.map.fsxjcyzj)
        if (!form.map.fcdjcrw) {
          this.$VxeMessage({ content: "请填写承担审评任务", status: "warning" });
          return;
        }
        // if (!form.map.fzzyj) {
        //   this.$VxeMessage({ content: "请填写审评组长意见及评分", status: "warning" });
        //   return;
        // }

        if (!form.map.fsxjcyzj) {
          this.$VxeMessage({
            content: "请填写实习个人总结",
            status: "warning",
          });
          return;
        }
      }
      if (this.type.indexOf('组长评价') !==-1) {
        if (!form.map.fzzyj) {
          this.$VxeMessage({ content: "请填写审评组长意见及评分", status: "warning" });
          return;
        }
        if (!form.map.fpj) {
          this.$VxeMessage({ content: "请选择评定等级", status: "warning" });
          return;
        }
      }
      if( this.type.indexOf('经办人审核') !== -1) {
          // if (!form.map.fpj) {
          //   this.$VxeMessage({ content: "请确定评定等级", status: "warning" });
          //   return;
          // }
      }
      this.loading = true
      submitFlow(form)
        .then((res) => {
          //console.log(res);
          if (res.success) {
            this.$VxeMessage({ content: res.message, status: "success" });
            this.modal.visabled = false;
            // this.$refs.xTable.resetEvent()
            this.reCommpVxeTable = Date.now()
          } else {
            this.$VxeMessage({ content: res.message, status: "error" });
          }
          this.loading = false
        })
        // .catch((error) => {
        //   this.$VxeMessage({ content: "未知异常", status: "error" });
        // });
    },
    getQueryPamas() {
      //console.log(this.saveForm);
      const { dbrid, opinion, smgflag,fsxjcyzj,fzzyj,fzzxm,fpj,fcdjcrw,ftjpj } = this.saveForm;
      const formData = {};
      formData.map = {fsxjcyzj,fzzxm,fzzyj,fpj,fcdjcrw,ftjpj};
      formData.jobid = this.jobid;
      formData.ybid = this.ybid;
      formData.nextNode = this.nextNode;
      formData.currentNode = this.currentNode;
      formData.dbrid = dbrid;
      formData.smgflag = smgflag;
      formData.opinion = opinion;
      return formData;
    },
    handlerBock() {
      const form = this.getQueryPamas();
      if (this.type.indexOf('组长评价') !==-1){
        form.opinion = form.map.fzzyj
        if (!form.map.fzzyj) {
          this.$VxeMessage({ content: "请填写审评组长退回意见", status: "warning" });
          return;
        }
      }
      if(!form.dbrid && this.type.indexOf('经办人审核') === -1) {
        if (!form.opinion) {
          this.$VxeMessage({ content: "请填写退回办理意见", status: "warning" });
          return;
        }
      }
      this.returnLoading = true
      returnUpperFlowJob(form)
        .then((res) => {
          if (res.success) {
            this.$VxeMessage({ content: res.message, status: "success" });
            this.modal.visabled = false;
            this.reCommpVxeTable = Date.now()
          } else {
            this.$VxeMessage({ content: res.message, status: "error" });
          }
          this.returnLoading = false
        })
        .catch((error) => {
          this.$VxeMessage({ content: "未知异常", status: "error" });
        });
    },
    handlerFormChange(form) {
      this.saveForm = {...this.saveForm,...form};
      //console.log(this.saveForm);
    },
  },
};
</script>

<style lang="less" scoped>
// @import "../assets/table.less";
@import url("../assets/table.less");
</style>
